package com.flow.manager.action;

import java.io.PrintWriter;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;
import org.apache.struts2.ServletActionContext;

import com.flow.model.BackInfo;
import com.flow.model.PagerModel;
import com.flow.model.PermissionMenu;
import com.flow.service.IPermissionMenuService;
import com.flow.tools.StringTools;

/**
 * 角色菜单控制器
 * 
 * @author wang
 * @2015-12-25 PermissionMenuAction.java
 * 
 */
public class PermissionMenuAction {

	BackInfo back = new BackInfo();

	/**
	 * 服务层引用
	 */
	private IPermissionMenuService<PermissionMenu> pmService;

	/**
	 * 添加菜单
	 */
	public String insertPermissionMenu() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String name = request.getParameter("pmName");
		String showName = request.getParameter("pmShowName");
		String namespace = request.getParameter("pmNamespace");
		String managerOrplatform = request.getParameter("pmMangerOrplatform");
		String status = request.getParameter("pmStatus");
		String remark = request.getParameter("remark");

		try {

			managerOrplatform = !StringTools.nullOrEmpty(managerOrplatform) ? "0" : managerOrplatform;
			status = !StringTools.nullOrEmpty(status) ? "0" : status;
			PermissionMenu pm = new PermissionMenu(name, showName, namespace,
					Integer.parseInt(managerOrplatform),
					Integer.parseInt(status), remark);
			if (pmService.insertPermissionMenu(pm))
				return "SUCCESS";

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	/**
	 * 修改菜单
	 */
	public String updatePermissionMenu() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("pmId");
		String name = request.getParameter("pmName");
		String showName = request.getParameter("pmShowName");
		String namespace = request.getParameter("pmNamespace");
		String managerOrplatform = request.getParameter("pmMangerOrplatform");
		String status = request.getParameter("pmStatus");
		String remark = request.getParameter("remark");

		try {

			managerOrplatform = !StringTools.nullOrEmpty(managerOrplatform) ? "0" : managerOrplatform;
			status = !StringTools.nullOrEmpty(status) ? "0" : status;
			PermissionMenu pm = new PermissionMenu(Integer.valueOf(id), name, showName, namespace, Integer.parseInt(managerOrplatform), Integer.parseInt(status), remark);
			if (pmService.updatePermissionMenu(pm))
				return "SUCCESS";

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";

	}

	/**
	 * 验证菜单名
	 */
	public String validateName(){
		
		HttpServletRequest request = ServletActionContext.getRequest();
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		String name = request.getParameter("pmName");

		try {

			if (pmService.vlidateName(name)) {
				back.setBackCode("000");
			} else {
				back.setBackCode("001");
			}
			request.setAttribute("back", back);
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			JSON json = JSONSerializer.toJSON(back);
			out.print(json);
			out.flush();
			out.close();
			return null;

		} catch (Exception e) {

			e.printStackTrace();
			
		}
		
		return "ERROR";
		
	}
	
	/**
	 * 输入转跳
	 */
	public String inputUpdate() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");

		try {

			PermissionMenu pm = pmService.findPermissionMenuById(id);
			request.setAttribute("pm", pm);
			return "SUCCESS";

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";

	}

	/**
	 * 验证namaspace
	 */
	public String validateNamescpace() {

		HttpServletRequest request = ServletActionContext.getRequest();
		ServletActionContext.getResponse().setCharacterEncoding("utf-8");
		String namespace = request.getParameter("pmNamespace");

		try {

			if (pmService.vlidateNamespace(namespace)) {
				back.setBackCode("000");
			} else {
				back.setBackCode("001");
			}
			request.setAttribute("back", back);
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			JSON json = JSONSerializer.toJSON(back);
			out.print(json);
			out.flush();
			out.close();
			return null;

		} catch (Exception e) {

			e.printStackTrace();
			
		}
		
		return "ERROR";

	}

	/**
	 * 查询角色菜单列表
	 */
	public String findAllPermissionMenu() {

		HttpServletRequest request = ServletActionContext.getRequest();

		try {

			List<PermissionMenu> pmList = pmService.findAllPermissionMenu();
			request.setAttribute("pmList", pmList);
			return null;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	/**
	 * 查询角色菜单并分页
	 */
	public String findAllPermissionMenuByPager() {

		HttpServletRequest request = ServletActionContext.getRequest();

		try {

			PagerModel pager = pmService.findAllPermissionMenuByPager();
			request.setAttribute("pager", pager);
			return null;

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	/**
	 * 删除角色菜单
	 */
	public String deletePermissionMenu() {

		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("id");

		try {

			PermissionMenu pm = pmService.findPermissionMenuById(id);
			if (pmService.deletePermissionMenu(pm))
				return "SUCCESS";

		} catch (Exception e) {

			e.printStackTrace();

		}

		return "ERROR";
	}

	public void setPmService(IPermissionMenuService<PermissionMenu> pmService) {
		this.pmService = pmService;
	}

}
